<!--
 * @Descripttion: 文件导出按钮
 * @version: 
 * @Author: hutao
 * @Date: 2021-09-02 09:03:43
 * @LastEditors: hutao
 * @LastEditTime: 2021-12-19 15:08:14
-->
<template>
  <span @click="exportExcel">
    <slot>
      <el-button type="primary">
        导出Excel
      </el-button>
    </slot>
  </span>
</template>
<script lang='ts'>
import { Component, Prop, Vue } from "vue-property-decorator";
import { AxiosRequestConfig, Method } from "axios";
import { _axios } from "vue-kst-auth";
/** 设置axios返回类型 */
Vue.config.productionTip = false;
interface State {
  /** 数据状态 */
  loading: boolean;
}
@Component
export default class HtExport extends Vue {
  /**  请求方式 */
  @Prop() method!: Method;
  /**  请求地址 */
  @Prop() url!: string;
  /**  请求参数 */
  @Prop() params?: object;
  /**  下载的文件名 */
  @Prop() fileName!: string;
  /**  下载之前的验证 */
  @Prop() exportBefore!: boolean;

  /** 数据 */
  state: State = {
    loading: false,
  };
  /** 生命周期 */
  /** 方法 */

  /** 导出方法 */
  exportExcel() {
    if (this.exportBefore !== false) {
      //return false;
      this.state.loading = true;
      let fileName = this.fileName || "未知文件名.xlsx";
      let config: AxiosRequestConfig = {
        responseType: "blob",
        params: {},
      };
      if (this.params) {
        config = this.params;
      }
      if (this.url) {
        if (this.method === "post") {
          _axios
            .post(this.url, config, { responseType: "blob" })
            .then((res) => {
              const content = res.data;
              if (!this.fileName) {
                const headers = res.headers["content-disposition"];
                if (!headers) {
                  this.$notify.warning("暂无数据导出");
                  return;
                }
                fileName = decodeURIComponent(
                  headers.split("filename*=UTF-8")[1]
                ).replace("''", "");
              }
              const blob = new Blob([content as any]);
              if ("download" in document.createElement("a")) {
                // 非IE下载
                const elink = document.createElement("a");
                elink.download = fileName;
                elink.style.display = "none";
                elink.href = URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                URL.revokeObjectURL(elink.href); // 释放URL 对象
                document.body.removeChild(elink);
              } else {
                // IE10+下载
                (navigator as any).msSaveBlob(blob, fileName);
              }
            })
            .finally(() => {
              this.state.loading = false;
            });
        } else {
          _axios
            .get(this.url, { responseType: "blob", params: config })
            .then((res) => {
              const content = res.data;
              if (!this.fileName) {
                const headers = res.headers["content-disposition"];
                fileName = decodeURIComponent(
                  headers.split("filename*=UTF-8")[1]
                ).replace("''", "");
              }
              const blob = new Blob([content as any]);
              if ("download" in document.createElement("a")) {
                // 非IE下载
                const elink = document.createElement("a");
                elink.download = fileName;
                elink.style.display = "none";
                elink.href = URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                URL.revokeObjectURL(elink.href); // 释放URL 对象
                document.body.removeChild(elink);
              } else {
                // IE10+下载
                (navigator as any).msSaveBlob(blob, fileName);
              }
            })
            .finally(() => {
              this.state.loading = false;
            });
        }
      }
    }
  }
  /** 监听 */
  /** 计算属性 */
}
</script>

<style lang='scss' scoped></style>